<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./bootstrap.css">
    <script src="./jquery-3.6.0.js"></script>
    <style>
        .good-name {
            white-space: nowrap;

            overflow: hidden;

            text-overflow: ellipsis;
        }
        body{
            background-image: url(./bj40150419.gif);
        }
       #xua a:hover{
            color: brown;
        }


        .kind p {
            text-align: center;
            margin-top: 5px;
            color: white;
        }

        .active {
            background-color: rgba(255,255,255,0.3);
        }

        .kinds h2 {
            margin-left: 200px;
            color: white;
          
        }
        .container{
            color: white;
        }
        nav{
            background-image: url(./bj40150419.gif);
        }
    </style>
</head>

<body>
    <!-- 导航部分 -->
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>

            </div>

            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">

                    <li class="dropdown">

                        <ul class="dropdown-menu">
                            <li><a href="#">Action</a></li>
                            <li><a href="#">Another action</a></li>
                            <li><a href="#">Something else here</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a href="#">Separated link</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a href="#">One more separated link</a></li>
                        </ul>
                    </li>
                </ul>
                <form class="navbar-form navbar-left">
                    <div class="form-group">
                        <input type="text" class="form-control xu1_2" placeholder="Search">
                    </div>
                    <button type="button" class="btn btn-default xu1_1">Submit</button>
                </form>
                <ul class="nav navbar-nav navbar-right  " id="xua">
                    <li><a href="#" id="xu1">登录</a></li>
                    <li><a href="#" id="xu5"><span id="span">用户</span><button onclick="get()">退出</button></a></li>
                    <li><a href="#" id="xu12">购物车</a></li>
                    <li><a href="#" id="xu23">收藏</a></li>

                    <li class="dropdown">


                    </li>
                </ul>
            </div><!-- /.navbar-collapse -->
        </div><!-- /.container-fluid -->
    </nav>
    <!-- 导航结尾部分 -->

    <div class="kinds">
        <h2>分类导航</h2>

        <div class="kinds-item" style="display: flex; margin-left: 205px;">


            <div class="kind" >
                <div class="img" style="width: 100px;height: 100px;">
                    <img style="width: 100%;height: 100%;"
                        src="https://img0.baidu.com/it/u=1476534091,504590892&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=400"
                        alt="">
                </div>
                <p>全部商品</p>
            </div>

            <div class="kind" >
                <div class="img" style="width: 100px;height: 100px;">
                    <img style="width: 100%;height: 100%;"
                        src="https://img0.baidu.com/it/u=1476534091,504590892&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=400"
                        alt="">
                </div>
                <p>全部商品</p>
            </div>
        </div>
    </div>


    <!-- 商品部分 -->
    <div class="container">

    </div>

    <!-- 商品结尾部分 -->
    <script>
        function getKinds(options) {
            $.ajax({
                type: "GET",
                url: "https://api.it120.cc/conner/shop/goods/category/all",
                data: options || {}
            }).then(res => {
                console.log(res);
                if (res.code == 0) {
                    var str = ""
                    res.data.forEach((i) => {
                        str += `
                    <div class="kind"  data-id=${i.id}>
                        <div class="img" style="width: 121px;height: 121px;">
                            <img style="width: 80%;height: 80%;   margin-left: 12px;margin-top: 12px;"
                                src="${i.icon}"
                                alt="">
                        </div>
                        <p >${i.name}</p>
                    </div>
                    `
                    })
                    var kindsItem = document.getElementsByClassName('kinds-item')
                    kindsItem[0].innerHTML = str
                }
            })
        }
        getKinds()
        // 点击切换分类数据
        $('.kinds-item').on("click", '.kind', function () {

            var categoryId = $(this).attr("data-id")
            $(this).siblings().removeClass('active')
            $(this).addClass('active')

            console.log(categoryId);
            getgood({
                categoryId
            })
        })

        //获取本地存储的数据
        var token = localStorage.getItem('token')
        var username = localStorage.getItem('username')
        var xu1 = document.querySelector('#xu1')
        var xu5 = document.querySelector('#xu5')
        var span = document.querySelector('#span')
        console.log(token)
        if (token != null) {
            span.innerHTML = username
            xu1.style.display = 'none'
            xu5.style.display = 'block'

        } else {
            xu1.style.display = 'block'
            xu5.style.display = 'none'

        }
        function get() {
            localStorage.removeItem('token')
            location.reload()
        }
        $('#xu12').on('click', function () {
            location.href = './cart.html'
        })
        $('#xu23').on('click', function () {
            location.href = './shouc.html'
        })
        $(function () {
            getgood()
        })





        function getgood(options) {
            $.ajax({
                type: 'post',
                url: 'https://api.it120.cc/conner/shop/goods/list/v2',
                data: options || {}


            }).then(res => {
                console.log(res)

                if (res.code == 0) {
                    var good = res.data.result
                    var str = ''


                    good.forEach(i => {
                        str += `
         
            <div class="col-sm-6 col-md-4 col-lg-3">
                        <div class="thumbnail">
                        <img class='s-img' style="width: 200px; height: 200px; "
                            src="${i.pic}"alt="">
                        <div class="caption">
                        <h3 class='good-name'>${i.name}</h3>
                        <p good-price>原价： <span>${i.minPrice}</span>元</p>
                        <p good-price1>现价： <span>${i.originalPrice}</span>元</p>
                        <p good-store>库存：<span>12134534件</span></p>
                     <p>
                        <a href="#" class="btn btn-primary" role="button" data-id=${i.id} number=${i.stores}>进入详情</a> 
                        <a href="#" class="btn btn-default col-sm-offset-1" role="button" id="xu2"data-id=${i.id} number=${i.numberFav}>加入购物车</a>
                     </p>
                </div>
                </div>
            </div>
        
                 `

                    });
                    document.querySelector('.container').innerHTML = str
                }
                else if (res.code == 700) {
                    document.querySelector('.container').innerHTML = '暂无数据'
                }

            })

        }
        $('.xu1_1').click(function () {
            console.log('1111');
            var ss = $('.xu1_2').val()
            console.log(ss);
            getgood({
                nameLike: ss
            })
        })
        getgood()

        $('.container').on('click', '.btn-primary', function () {
            $id = ($(this).attr('data-id'))
            sessionStorage.setItem('id', $id)
            location.href = './datal.html'
        })

        $('#xu1').on('click', function () {
            location.href = './login.html'
        })
        $('.container').on('click', '#xu2', function () {

            $id = ($(this).attr('data-id'))
            $number = $(this).attr('number')
            sessionStorage.setItem('id', $id)
            sessionStorage.setItem('number', $number)
            var data = {
                number: $number,
                goodsId: $id,
                token: localStorage.getItem('token')
            }
            console.log(data)
            $.ajax({
                url: 'https://api.it120.cc/conner/shopping-cart/add',
                type: 'post',
                data

            }).then(res => {
                console.log(res)
                if (res.code == 0) {
                    var bool = confirm('是否前往购物车页')
                    bool && (location.href = './cart.html')
                } else {
                    location.href = './login.html'
                }
            })

            //   location.href='./cart.html'
        })
    </script>

</body>

</html>